package com.hlhj.funlike_business.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.example.mymvp.mvp.BaseFgm;
import com.flyco.tablayout.SlidingTabLayout;
import com.hlhj.funlike_business.CustView.MyViewPager;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.AddCommentyAty;
import com.hlhj.funlike_business.activity.adapter.FoodTitleAdp;
import com.hlhj.funlike_business.bean.FoodTypeBean;
import com.hlhj.funlike_business.presenter.MineFoodFgmPresenter;
import com.hlhj.funlike_business.views.MineFoodFgmView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MineFoodFragment extends BaseFgm<MineFoodFgmView, MineFoodFgmPresenter> implements MineFoodFgmView {
    private TextView tv_addfood;
    private MyViewPager food_viewpager;
    private SlidingTabLayout tabLayout;
    private FoodTitleAdp foodTitleAdp;
    private List<String> stringList;
    private List<Fragment> fragments;
    private FoodFragment foodFragment;

    @Override
    public void doBeforInit() {

    }

    @Override
    public int getContentId() {
        return R.layout.fgm_minefood;
    }

    @NotNull
    @Override
    public MineFoodFgmView bindView() {
        return this;
    }

    @NotNull
    @Override
    public MineFoodFgmPresenter createPresenter() {
        return new MineFoodFgmPresenter();
    }

    @Override
    public void initView() {
        food_viewpager = rootView.findViewById(R.id.food_viewpager);
        tabLayout = rootView.findViewById(R.id.tabLayout);
        tv_addfood = rootView.findViewById(R.id.tv_addfood);

    }

    @Override
    public void logicStart() {
        stringList = new ArrayList<>();
        fragments = new ArrayList<>();
        stringList.add("全部商品");     //默认为0
        fragments.clear();
        for (int i = 0; i < stringList.size(); i++) {
            foodFragment = FoodFragment.newInstance(0);
            fragments.add(foodFragment);
        }
        foodTitleAdp = new FoodTitleAdp(getChildFragmentManager(), stringList, fragments);
        food_viewpager.setAdapter(foodTitleAdp);
        tabLayout.setViewPager(food_viewpager);

        tabLayout.setTextSelectColor(getContext().getResources().getColor(R.color.tvblue));
        tabLayout.setTextUnselectColor(getContext().getResources().getColor(R.color.black));
        tabLayout.setIndicatorColor(getContext().getResources().getColor(R.color.tvblue));
        tabLayout.setIndicatorWidth(30);

        getPresenter().LoadFoodList(getContext());
    }

    @Override
    public void setListener() {
        tv_addfood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddCommentyAty.class);
                intent.putExtra("id",0);
                intent.putExtra("type",0);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void showFoodList(List<FoodTypeBean.DataBean> dataBeans) {
        if (dataBeans!=null){
            stringList.clear();
        }
        dataBeans.add(0,new FoodTypeBean.DataBean(0,"全部商品"));
        for (FoodTypeBean.DataBean foodTypeBean : dataBeans) {
            stringList.add(foodTypeBean.getType_name());
        }
        fragments.clear();
        for (int i = 0; i < dataBeans.size(); i++) {
            foodFragment = FoodFragment.newInstance(dataBeans.get(i).getId());
            fragments.add(foodFragment);
        }
        foodTitleAdp = new FoodTitleAdp(getChildFragmentManager(),stringList,fragments);
        food_viewpager.setAdapter(foodTitleAdp);
        tabLayout.setViewPager(food_viewpager);
    }
}
